//
//  NewsVC.swift
//  旅航ios
//
//  Created by tulip on 2021/4/21.
//

import UIKit
import Parchment
import HandyJSON
class NewsVC: UIViewController {
    
    var dataArr: [String] = []
    
    var newsData:NewsMainModel?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "资讯"
        
        getdata()
        
    }
    
    
    func getdata(){
        NetWorkRequest(API1.serviceGetNews, completion: {[weak self] (responseString) -> (Void) in
            guard let model = JSONDeserializer<NewsMainModel>.deserializeFrom(json: "\(responseString)") else {
                return
            }
            self?.newsData = model
            self?.searchIndex()
            self?.setUppagingView()
        }, failed: { (failedResutl) -> (Void) in
            print("服务器返回code不为0000啦~\(failedResutl)")
        }, errorResult: { () -> (Void) in
            print("网络异常,超时")
        })
    }
    
    
    func searchIndex(){
        for model in newsData!.data!{
            dataArr.append(model.name)
        }
    }
    
    
    func setUppagingView() {
        addChild(self.pagingView)
        self.view.addSubview(self.pagingView.view)
        self.pagingView.view.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(45)
        }
        self.pagingView.didMove(toParent: self)
        self.pagingView.select(index: 0)

    }
    
    lazy var pagingView: PagingViewController = {
        let pagingViewController = PagingViewController()
        pagingViewController.dataSource = self
        pagingViewController.delegate = self
        pagingViewController.menuItemSize = .selfSizing(estimatedWidth: 60, height: 40)
        pagingViewController.font = UIFont.systemFont(ofSize: 14)
        pagingViewController.selectedFont = UIFont.boldSystemFont(ofSize: 17)
        pagingViewController.selectedTextColor = UIColor.init(hexString: "333333")
        pagingViewController.indicatorColor = UIColor.init(hexString: "FF8A00")
        pagingViewController.menuItemLabelSpacing = 20
        pagingViewController.indicatorOptions = .visible(
          height: 1,
          zIndex: Int.max,
          spacing: UIEdgeInsets.init(top: 0, left: 12, bottom: 0, right: 12),
          insets: UIEdgeInsets.zero
        )
        return pagingViewController
    }()

}

extension NewsVC: PagingViewControllerDataSource,PagingViewControllerDelegate {
  func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
   
    return PagingIndexItem(index: index, title: dataArr[index])
    
  }
  
  func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
    
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsSubViewController") as! NewsSubViewController
    vc.model = newsData?.data![index].list
     return vc
  }
  
  func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
    return self.dataArr.count
  }
    
    func pagingViewController(
      _ pagingViewController: PagingViewController,
      didScrollToItem pagingItem: PagingItem,
      startingViewController: UIViewController?,
      destinationViewController: UIViewController,
        transitionSuccessful: Bool){
        if transitionSuccessful {
//            let item = pagingItem as! PagingIndexItem
//            if let array = self.leftDatas {
//                let dic = array[item.index] as! Dictionary<String,Any>
//                self.category_id = "\(dic["id"] ?? "")"
//                self.sonArray = dic["son"] as? Array<Any>
//                NotificationCenter.default.post(name: NSNotification.Name("ClassificationNotification"), object: nil, userInfo: ["id":self.category_id,"list":self.sonArray ?? [],"search":self.search])
//            }
//
        }
    }
}
